package com.yunfangdata.web.controller;


import com.yunfangdata.domain.Employee;
import com.yunfangdata.query.QueryObject;
import com.yunfangdata.service.IEmployeeService;
import com.yunfangdata.util.JsonResult;
import com.yunfangdata.util.PageResult;
import com.yunfangdata.util.PermissionName;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @RequiresPermissions("employee:index")
    @PermissionName("员工首页")
    @RequestMapping("")
    public String index(){
        return "employee/list";
    }

    @RequiresPermissions("employee:list")
    @PermissionName("员工列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        return employeeService.query(qo);
    }

    @RequiresPermissions("employee:update")
    @PermissionName("员工更新")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Employee employee){
        JsonResult result = new JsonResult();
        try {
            employeeService.update(employee);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("操作失败");
        }
        return result;

    }

    @RequiresPermissions("employee:changeState")
    @PermissionName("员工状态")
    @RequestMapping("changeState")
    @ResponseBody
    public JsonResult changeState(Long id) {
        JsonResult result = new JsonResult();
        try {
            employeeService.changeState(id);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult("操作失败");
        }
        return result;
    }

    //修改密码
    @RequestMapping("changePassword")
    @ResponseBody
    public JsonResult changePassword(String username,String password,String newPassword){
        JsonResult result = new JsonResult();
        try {
            employeeService.changePassword(username,password,newPassword);
        } catch (Exception e) {
            e.printStackTrace();
            result = new JsonResult(e.getMessage());
        }
        return result;
    }

    //登录头像回显
    @RequestMapping("queryPortraitByUsername")
    @ResponseBody
    public Employee queryPortraitByUsername(String username){
        Employee employee = null;
        try {
            employee = employeeService.queryPortraitByUsername(username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return employee;
    }

    //所有经理
    @RequestMapping("Managers")
    @ResponseBody
    public List<Employee> Managers(){
        return employeeService.queryEmployeeByRoleManager();
    }

    @RequestMapping("/queryDates")
    @ResponseBody
    public List<Employee> queryDates(){
        return employeeService.list();
    }

    //查询所有员工
    @RequestMapping("allselect")
    @ResponseBody
    public List<Employee> allselect(){
        return employeeService.list();
    }

    @RequestMapping("queryCurrentUserByEmpUsernameAndPassword")
    @ResponseBody
    public Employee queryCurrentUserByEmpUsernameAndPassword(String userName,String password){

        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        String psws = new Md5Hash(password,employee.getUsername(),3).toString();
        return employeeService.queryCurrentUserByEmpUsernameAndPassword(employee.getUsername(),psws);
    }
}
